package com.roadjava.GDOUGym.service.impl;

import com.roadjava.GDOUGym.bean.dto.EquipmentDTO;
import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.entity.EquipmentDO;
import com.roadjava.GDOUGym.bean.entity.PlaceDO;
import com.roadjava.GDOUGym.bean.entity.RecordDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.EquipmentConvert;
import com.roadjava.GDOUGym.convert.SiteConvert;
import com.roadjava.GDOUGym.mapper.EquipmentMapper;
import com.roadjava.GDOUGym.mapper.SiteMapper;
import com.roadjava.GDOUGym.service.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class EquipmentServiceImpl implements EquipmentService {
    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private EquipmentConvert equipmentConvert;

    @Override
    public ResultDTO<List<EquipmentDTO>> find(EquipmentDTO dto) {
        List<EquipmentDO> dos = equipmentMapper.find(dto);
        List<EquipmentDTO> dtos = equipmentConvert.dos2dtos(dos);
        long total = equipmentMapper.selectCount(dto);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(EquipmentDTO equipmentDTO) {
        EquipmentDTO dto = new EquipmentDTO();
        String no;
        List<EquipmentDO> equipmentDOList = equipmentMapper.find(dto);
        if(equipmentDOList.size() == 0){
            no ="0001";
        }else{
            String endNo = equipmentDOList.get(equipmentDOList.size()-1).getEquipmentID();
            long total = Integer.parseInt(endNo)+1;
            no = "";
            if(total>=0 && total<10){
                no = "000"+total;
            }else if(total>=10 && total<100){
                no = "00"+total;
            }else if(total>=100 && total<1000){
                no = "0"+total;
            }else{
                no = total+"";
            }
        }
        // 把dto转为do对象
        EquipmentDO equipmentDO = equipmentConvert.dto2do(equipmentDTO);
        equipmentDO.setEquipmentID(no);
        int result = equipmentMapper.add(equipmentDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("添加成功");
        }else {
            return ResultDTO.buildSuccess("添加失败");
        }
    }

    @Override
    public ResultDTO<String> change(EquipmentDTO equipmentDTO) {
        // 把dto转为do对象
        EquipmentDO equipmentDO = equipmentConvert.dto2do(equipmentDTO);
        int result = equipmentMapper.change(equipmentDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(EquipmentDTO equipmentDTO) {
        // 把dto转为do对象
        EquipmentDO equipmentDO = equipmentConvert.dto2do(equipmentDTO);
        int result = equipmentMapper.delete(equipmentDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }
}
